package com.feiyu.simplesort;

/**
 * 冒泡排序
 *
 * @Author nanshouxiao@teacher.com.cn
 * @Date 2017-10-26 14:39
 */
public class BubbleSort extends BaseSort {



    /**
     * 每次都和它排序之后的元素比较，如果比后面的大，则将两个值替换
     */
    private int[] bubbleSort(int[] array) {
        for(int i = 0; i < array.length - 1; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[i] < array[j]) {
                    int temp = array[i];
                    array[i] = array[j];;
                    array[j] = temp;
                }
            }
        }
        return array;
    }

    private int[] bubbleSort2(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            for(int j = 0; j < array.length - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j + 1];
                    array[j + 1] = array[j];;
                    array[j] = temp;
                }
            }
        }
        return array;
    }

    @Override
    public int[] sort(int[] array) {
        return this.bubbleSort2(array);
    }
}
